package com.oba.king.controlmanage;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.event.AbortProcessingException;
import javax.faces.model.SelectItem;

import com.icesoft.faces.component.paneltabset.TabChangeEvent;
import com.oba.base.dao.DBDataModel;
import com.oba.base.service.BaseBean;
import com.oba.king.controlmanage.proxyproduct.ProxyProductDetailsBean;
import com.oba.king.datalib.CompanyInfo;
import com.oba.king.datalib.Customer;
import com.oba.king.login.UserInfo;

/**
 * 
 * @author 大饼 
 * 代理商协议信息管理
 */
public class ProxyContractBean extends
		BaseBean<ProxyContract, ProxyContractService> {

	private DBDataModel proxyContractData = DBDataModel.create();
	private ProxyContract proxyContract;
	
	private ProxyProductDetailsBean proxyProductDetailsBean;
	
	private boolean isAudit;

	// 查询 条件
	private ProxyContractCondition proxyContractCondition;
	// 客户信息选择列表
	private List<SelectItem> customerList;
	private List<SelectItem> queryCustomerList;
	
	private int focusIndex;
	
	//是否在录入页面 显示返回按钮
	private boolean showBack;
	
	/**
	 * Tabbed placement, possible values are "top" and "bottom", the default is
	 * "bottom".
	 */
	private String tabPlacement = "top";
//	private boolean contractVisible =true;
//	private boolean productVisible;

	public void doAddRow() {
		CompanyInfo companyInfo=new CompanyInfo();
		companyInfo.setNewBo(false);
		companyInfo=getService().getDao().findAllCompany().get(0);
		proxyContract = new ProxyContract();
		proxyContract.setAuditingStatus(1);
		proxyContract.setCompanyInfo(companyInfo);
		proxyContract.setNewBo(true);
		setFocusIndex(0);
		goTarget("/proxycontract/addProxyContract.jspx");
	}

	public void initCustomerList() {
		customerList = new ArrayList<SelectItem>();

		for (Customer a : getService().getDao().findAllCustomer()) {
			customerList.add(new SelectItem(a, a.getCustomerName()));
		}
	}

	private void initQueryCustomerList() {
		queryCustomerList = new ArrayList<SelectItem>();
		queryCustomerList.add(new SelectItem(null, "全部"));
		queryCustomerList.addAll(customerList);
	}

	public void doEditRow() {
		if (proxyContractData.isRowAvailable()) {
			proxyContract = (ProxyContract) proxyContractData.getRowData();
			if(proxyContract.getAuditingStatus()!=1){
				proxyContract.setAuditingStatus(4);
			}
			proxyProductDetailsBean.setProxyContract(proxyContract);
			proxyProductDetailsBean.initDetailsList(proxyContract);
			setFocusIndex(0);
			goTarget("/proxycontract/addProxyContract.jspx");
		}
	}

	public void doDeleteRow() {
		if (proxyContractData.isRowAvailable()) {
			proxyContract = (ProxyContract) proxyContractData.getRowData();
			getService().getDao().deleteContract(proxyContract);
			doFind();
		}
	}
	
	/**
	 * 返回代理协议页面
	 */
	public void returnback(){
		goTarget("/proxycontract/proxycontract.jspx");
	}

	public void auditRow(){
		if (proxyContractData.isRowAvailable()) {
			proxyContract = (ProxyContract) proxyContractData.getRowData();
			proxyProductDetailsBean.setProxyContract(proxyContract);
			proxyProductDetailsBean.initDetailsList(proxyContract);
			goTarget("/proxycontract/auditProxyContract.jspx");
		}
	}
	
	public void auditReject(){
		proxyContract.setAuditingStatus(3);
		getService().save(proxyContract);
		doFindAudit();
		goTarget("/proxycontract/proxycontract.jspx");
	}
	
	public void auditPass(){
		proxyContract.setAuditingStatus(2);
		getService().save(proxyContract);
		doFindAudit();
		goTarget("/proxycontract/proxycontract.jspx");
	}
	
	public void doFindAudit(){
		proxyContractData.find(getService().getDao().findNeedAudit());
	}

	public void doSaveRow() {
		// TODO
		//getService().save(proxyContract.getAccountValue());
		proxyContract.setUserInfo(this.getSysUser());
		getService().save(proxyContract);
		proxyProductDetailsBean.setProxyContract(proxyContract);
		proxyProductDetailsBean.initDetailsList(proxyContract);
		
		setFocusIndex(1);
	}

	@Override
	public String doFind() {
		proxyContractData.find(getService().getDao().findAll());
		return null;
	}

	public void clearProxyContractCondition() {
		proxyContractCondition = new ProxyContractCondition();
	}

	public void findByProxyContract() {
		if(isAudit){
		proxyContractData.find(getService().getDao().findAuditProxyContract(
				proxyContractCondition));
		}else{
			proxyContractData.find(getService().getDao().findByProxyContract(
					proxyContractCondition));
		}
	}

	@Override
	public void init() {
		showBack=true;
		isAudit=false;
		proxyContractData.find(getService().getDao().findAll());
		proxyContract = new ProxyContract();
		proxyContractCondition = new ProxyContractCondition();
		initCustomerList();
		initQueryCustomerList();
		isAudit=false;
	}
	
	public void initAudit(){
		showBack=true;
		isAudit=true;
		proxyContractData.find(getService().getDao().findNeedAudit());
		proxyContract = new ProxyContract();
		proxyContractCondition = new ProxyContractCondition();
		initCustomerList();
		initQueryCustomerList();
	}
	
	public void initAdd(){
		showBack=false;
		isAudit=false;
		initCustomerList();
		initQueryCustomerList();
		CompanyInfo companyInfo=new CompanyInfo();
		companyInfo.setNewBo(false);
		companyInfo=getService().getDao().findAllCompany().get(0);
		proxyContract = new ProxyContract();
		proxyContract.setAuditingStatus(1);
		proxyContract.setCompanyInfo(companyInfo);
		proxyContract.setNewBo(true);
		setFocusIndex(0);
	}



	public DBDataModel getProxyContractData() {
		return proxyContractData;
	}



	public void setProxyContractData(DBDataModel proxyContractData) {
		this.proxyContractData = proxyContractData;
	}



	public ProxyContract getProxyContract() {
		return proxyContract;
	}



	public void setProxyContract(ProxyContract proxyContract) {
		this.proxyContract = proxyContract;
	}



	public ProxyContractCondition getProxyContractCondition() {
		return proxyContractCondition;
	}



	public void setProxyContractCondition(
			ProxyContractCondition proxyContractCondition) {
		this.proxyContractCondition = proxyContractCondition;
	}



	public List<SelectItem> getCustomerList() {
		return customerList;
	}



	public void setCustomerList(List<SelectItem> customerList) {
		this.customerList = customerList;
	}



	public List<SelectItem> getQueryCustomerList() {
		return queryCustomerList;
	}



	public void setQueryCustomerList(List<SelectItem> queryCustomerList) {
		this.queryCustomerList = queryCustomerList;
	}
	
	public String getTabPlacement() {
		return tabPlacement;
	}

	public void setTabPlacement(String tabPlacement) {
		this.tabPlacement = tabPlacement;
	}

	public ProxyProductDetailsBean getProxyProductDetailsBean() {
		return proxyProductDetailsBean;
	}

	public void setProxyProductDetailsBean(
			ProxyProductDetailsBean proxyProductDetailsBean) {
		this.proxyProductDetailsBean = proxyProductDetailsBean;
	}
	
	/**
     * Called when the table binding's tab focus changes.
     *
     * @param tabChangeEvent used to set the tab focus.
     * @throws AbortProcessingException An exception that may be thrown by event
     *                                  listeners to terminate the processing of the current event.
     */
    public void processTabChange(TabChangeEvent tabChangeEvent)
            throws AbortProcessingException {
    	focusIndex = tabChangeEvent.getNewTabIndex();
    }

	public int getFocusIndex() {
		return focusIndex;
	}

	public void setFocusIndex(int focusIndex) {
		this.focusIndex = focusIndex;
	}



	public boolean isAudit() {
		return isAudit;
	}



	public void setAudit(boolean isAudit) {
		this.isAudit = isAudit;
	}

	public boolean isShowBack() {
		return showBack;
	}

	public void setShowBack(boolean showBack) {
		this.showBack = showBack;
	}

}
